/**
 * Created by George on 2017/9/18.
 */
var cascader = function (obj) {
    'use strict';
    var self = this;
    this.asyncLoad = function () {
        var value = self.getSlug();
        var uri = obj.uri.replace('-slug-', value);
        $.ajax({
            url: uri,
            dataType: 'json'
        }).done(function (data, status, response) {
            self.addItem(data);
        }).fail(function (response, status, message) {
            console.log(message);
        });
    };
    this.addItem = function (nodes) {
        obj.me.empty();
        for (var i in nodes) {
            var node = nodes[i];
            var option = new Option(node.value, node.key);
            obj.me.append(option);
        }
        if (!ace.vars['touch']) {
            $('.chosen-select').chosen({allow_single_deselect: true});
        }
    }

    self.getSlug = function () {
        if (obj.parent) {
            console.log(obj.parent.val());
            return obj.parent.val() | "0";
        }
        return "0";
    };

    if (obj.parent) {
        obj.parent.on('change', function (event, node) {
            self.asyncLoad();
        })
    }
}
